var GlobalVideoPlayer = {
    swfIdName: "",
    playerRef: null,
    playerX: 0,
    playerY: 0,
    isOverMenu: false,
    initialized: false,
    initialize: function(swfIdName) {
        GlobalVideoPlayer.swfIdName = swfIdName;
        GlobalVideoPlayer.initialized = true;
    },
    callbackReady: function () {

        if (GlobalVideoPlayer.swfIdName == "") {
            return;
        }

        GlobalVideoPlayer.playerRef = window.document[GlobalVideoPlayer.swfIdName];
        GlobalVideoPlayer.elementRef = document.getElementById(GlobalVideoPlayer.swfIdName);
        
        GlobalVideoPlayer.playerX = GlobalVideoPlayer.findPosX(GlobalVideoPlayer.elementRef);
        GlobalVideoPlayer.playerY = GlobalVideoPlayer.findPosY(GlobalVideoPlayer.elementRef);
        
        isOverVideo = false;

        if(!(document.attachEvent)) {
            window.addEventListener("DOMMouseScroll", GlobalVideoPlayer.handleWheel, false);
            window.onmousewheel = GlobalVideoPlayer.handleWheel;
        } else {
            window.onmousewheel = GlobalVideoPlayer.handleWheel;
        }
    },
    handleWheel: function(event) {
        if (!GlobalVideoPlayer.isOverMenu || GlobalVideoPlayer.swfIdName == "") {
            return;
        }
        
        event.preventDefault();

        var playerRef = window.document[GlobalVideoPlayer.swfIdName];

        if (playerRef) {

            var docX = 0;
            var docY = 0;

            if (event.pageX == null) {
              // IE case
              var d = (document.documentElement &&
                      document.documentElement.scrollLeft != null) ?
                      document.documentElement : document.body;
              docX = event.clientX + d.scrollLeft;
              docY = event.clientY + d.scrollTop;
            } else {
              // all other browsers
              docX = event.pageX;
              docY = event.pageY;
            }

            var o = {
                x:        docX - GlobalVideoPlayer.playerX,
                y:        docY - GlobalVideoPlayer.playerY,
                delta:    (event.wheelDelta === undefined) ? event.detail : (event.wheelDelta / -40),
                ctrlKey:  event.ctrlKey, altKey: event.altKey,
                shiftKey: event.shiftKey
            }
            playerRef.handleWheel(o);
        }
    },
    handleMouseOver: function() {
        if (GlobalVideoPlayer.swfIdName == "") {
            return;
        }

        GlobalVideoPlayer.isOverMenu = true;
    },
    getAgeGateStatus: function() {
        var isAgeAppropriate = GlobalVideoPlayer.readCookie("ageAppropriate");

        if (isAgeAppropriate == "false") {
            return "unavailable";
        } else if (isAgeAppropriate == "true") {
            return "skip";
        }

        return "prompt";
    },
    createCookie: function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        } else {
            var expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    readCookie: function(name) {
        name = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return null;
    },
    eraseCookie: function(name) {
        GlobalVideoPlayer.createCookie(name, "", -1);
    },
    handleMouseOut: function() {
        if (GlobalVideoPlayer.swfIdName == "") {
            return;
        }

        GlobalVideoPlayer.isOverMenu = false;
    },
    /**
     * findPosX and findPosY adopted from http://blog.firetree.net/2005/07/04/javascript-find-position/
     * Authors: Peter-Paul Koch & Alex Tingle
     */
    findPosX: function(obj) {
        var currentLeft = 0;
        if (obj.offsetParent) {
            while(1) {
                currentLeft += obj.offsetLeft;
                if (!obj.offsetParent) {
                    break;
                }
                obj = obj.offsetParent;
            }
        } else if(obj.x) {
            currentLeft += obj.x;
        }
        return currentLeft;
    },
    findPosY: function(obj) {
        var currentTop = 0;
        if (obj.offsetParent) {
            while(1) {
                currentTop += obj.offsetTop;
                if (!obj.offsetParent) {
                    break;
                }
                obj = obj.offsetParent;
            }
        } else if(obj.y) {
            currentTop += obj.y;
        }
        return currentTop;
    }
};